package com.juntu.loginlib.util;

import android.app.ActivityThread;
import android.app.AppGlobals;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import com.juntu.loginlib.contact.LoginConstant;

import java.util.UUID;

/**
 * Created by cj on 2021/2/19.
 * Email:codesexy@163.com
 * Function:
 * desc:
 */
public class LoginUtil {


    public static void saveSp(String key, Object value) {
        SharedPreferences preferences = getSp();
        SharedPreferences.Editor edit = preferences.edit();
        if (value instanceof String) {
            edit.putString(key, String.valueOf(value));
        } else if (value instanceof Boolean) {
            edit.putBoolean(key, (Boolean) value);
        } else if (value instanceof Float) {
            edit.putFloat(key, (Float) value);
        } else if (value instanceof Integer) {
            edit.putInt(key, (Integer) value);
        } else if (value instanceof Long) {
            edit.putLong(key, (Long) value);
        }
        edit.apply();
    }

    public static String getStr(String key) {
        return getSp().getString(key, "");
    }

    public static Boolean getBoolean(String key) {
        return getSp().getBoolean(key, false);
    }


    /**
     * 反射获取application对象
     *
     * @return application
     */
    public static Application getApplication() {
        try {
            //兼容android P，直接调用@hide注解的方法来获取application对象
            Application app = ActivityThread.currentApplication();
            Log.e("LoginUtil", "getApplication0:" + app);
            if (app != null) {
                return app;
            }
        } catch (Exception e) {
        }
        try {
            //兼容android P，直接调用@hide注解的方法来获取application对象
            Application app = AppGlobals.getInitialApplication();
            Log.e("LoginUtil", "getApplication1:" + app);
            if (app != null) {
                return app;
            }
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 这个东西解决本地获取
     *
     * @return
     */
    public static String getDeviceId() {
        Application application = getApplication();
        SharedPreferences sp = application.getSharedPreferences(application.getPackageName(), Context.MODE_PRIVATE);
        String deviceId = sp.getString(LoginConstant.DEVICE_ID, "");
        if (TextUtils.isEmpty(deviceId)) {
            String uuid = UUID.randomUUID().toString().replace("-", "");
            sp.edit().putString(LoginConstant.DEVICE_ID, uuid).apply();
            return uuid;
        } else {
            return deviceId;
        }

    }

    private static SharedPreferences getSp() {
        Application application = getApplication();
        SharedPreferences preferences = application.getSharedPreferences(application.getPackageName(), Context.MODE_PRIVATE);
        return preferences;
    }

    /**
     * 获取设备厂商
     * @return
     */
    public static String getDeviceManufacturer() {
        return Build.MANUFACTURER;
    }

    /**
     * 获取手机设备名
     *
     * @return  手机设备名
     */
    public static String getSystemDevice() {
        return Build.DEVICE;
    }
}
